package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConst;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author 黑马程序员
 * @Company http://www.ithiema.com
 * @Version 1.0
 */
@RestController
@RequestMapping("/ordersetting")
@Slf4j
public class OrderSettingController {


    @Reference
    OrderSettingService orderSettingService;


    /**
     * 批量导入预约设置信息
     * @param excelFile  excel 对象
     * @return
     */
    @RequestMapping("/upload")
    public Result upload(MultipartFile excelFile){
        //把从excel中的读取到的数据添加到集合中
        List<OrderSetting> orderSettingList = new ArrayList<>();
        try {
            //获取流对象
            InputStream inputStream = excelFile.getInputStream();
            //获取工作薄对象
            XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
            //获取工作表对象
            XSSFSheet sheet = workbook.getSheetAt(0);
            //获取excel中的数据
            //最后一行的行号
            int lastRowNum = sheet.getLastRowNum();
            // 从第二行开始（第一行是表头）
            for (int i = 1; i <= lastRowNum; i++) {
                //每行对应的OrderSetting对象
                OrderSetting orderSetting = new OrderSetting();
                //获取第i行
                XSSFRow row = sheet.getRow(i);
                //获取第i行的第一个单元格，是日期
                XSSFCell cell = row.getCell(0);
                //获取单元格数据
                Date orderDate = cell.getDateCellValue();
                //将字符串数据转换为日期
                orderSetting.setOrderDate(orderDate);

                //获取第i行的第二个单元格，是改日期的可预约人数
                XSSFCell cell1 = row.getCell(1);
                if(cell1.getCellType() == Cell.CELL_TYPE_NUMERIC){
                    cell1.setCellType(Cell.CELL_TYPE_STRING);
                }
                //获取单元格数据，并转换为整数
                String numberString = cell1.getStringCellValue();
                int number = Integer.parseInt(numberString);
                orderSetting.setNumber(number);

                //把预约设置对添加到集合
                orderSettingList.add(orderSetting);
            }
            log.debug("orderSettingController:upload"+orderSettingList);

            orderSettingService.addOrderSettingList(orderSettingList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConst.IMPORT_ORDERSETTING_FAIL);
        }
        return new Result(true,MessageConst.IMPORT_ORDERSETTING_SUCCESS);
    }

    @RequestMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(String date){
        log.debug("SetmealController:getOrderSettingByMonth:" + date);
        List<OrderSetting> orderSettingList = orderSettingService.getOrderSettingByMonth(date);
        //把查询到数据转换成页面需要的数据模型
        List<Map<String,Object>> mapList = new ArrayList<>();
        for (OrderSetting orderSetting : orderSettingList) {
            //一个ordersetting对应一个Map集合
            Map<String,Object> map = new HashMap<>();
            // map.put("date", orderSetting.getOrderDate().getDate());
            map.put("date", new SimpleDateFormat("dd").format(orderSetting.getOrderDate()));
            map.put("number",orderSetting.getNumber());
            map.put("reservations",orderSetting.getReservations());
            //把map添加到集合中
            mapList.add(map);
        }
        log.debug("获取预约设置信息成功！！！");
        log.debug(mapList.toString());
        return new Result(true,MessageConst.GET_ORDERSETTING_SUCCESS, mapList);
    }

    @RequestMapping("/saveOrEdit")
    public Result saveOrEdit(@RequestBody OrderSetting orderSetting){
        log.debug("OrderSettingController:saveorEdit:" + orderSetting);
        orderSettingService.add(orderSetting);
        log.debug("更新预约设置信息成功！！");
        return new Result(true,MessageConst.ORDERSETTING_SUCCESS);

    }
}
